package com.example.personalaccount.handler;

import com.example.personalaccount.entity.User;
import com.example.personalaccount.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

@Component
public class SecurityGetUserUtil {
    @Autowired
    private UserRepository userRepository;

    public User getUser()
    {
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        User user = userRepository.getUserByUsername(name);
        if (user == null) throw new RuntimeException("login not found");
        return user;
    }
}
